<?php

class AdminController extends Controller
{
	public $defaultAction = 'admin';
	public $layout='//layouts/simple';
	
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return CMap::mergeArray(parent::filters(),array(
			'accessControl', // perform access control for CRUD operations
		));
	}
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete','create','update','view'),
				'users'=>UserModule::getAdmins(),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new User('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['User']))
            $model->attributes=$_GET['User'];

        $this->render('index',array(
            'model'=>$model,
        ));
		/*$dataProvider=new CActiveDataProvider('User', array(
			'pagination'=>array(
				'pageSize'=>Yii::app()->controller->module->user_page_size,
			),
		));

		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));//*/
	}


	/**
	 * Displays a particular model.
	 */
	public function actionView()
	{
		$model = $this->loadModel();
		$this->render('view',array(
			'model'=>$model,
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new User;
		$profile=new Profile;
		$this->performAjaxValidation(array($model,$profile));
        if(isset($_REQUEST['Profile'])){
            $profile_imei = Profile::model()->findByAttributes(array('imei'=>$_REQUEST['Profile']['imei']));
        }
        if(isset($profile_imei->user_id) && $profile_imei->user_id!=$profile->user_id){
            $profile->imei = $_REQUEST['Profile']['imei'];
            $profile->addError('imei','imei号重复');
        }else if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			$model->activkey=Yii::app()->controller->module->encrypting(microtime().$model->password);
			$profile->attributes=$_POST['Profile'];
			$profile->user_id=0;
			if($model->validate()&&$profile->validate()) {
				$model->password=Yii::app()->controller->module->encrypting($model->password);
				if($model->save()) {
					$profile->user_id=$model->id;
					$profile->save();

                    //超级用户权限
                    $as = Authassignment::model()->findByAttributes(array('userid'=>$model->id));
                    if($model->superuser==1){
                        if(empty($as)){
                            $as = new Authassignment;
                            $as->itemname = 'Admin';
                            $as->userid = $model->id;
                            $as->bizrule = null;
                            $as->data = 'N;';
                        }else{
                            $as->itemname = 'Admin';
                        }
                    }elseif($model->superuser==0){
                        if(empty($as)){
                            $as = new Authassignment;
                            $as->itemname = 'Poster';
                            $as->userid = $model->id;
                            $as->bizrule = null;
                            $as->data = 'N;';
                        }else{
                            $as->itemname = 'Poster';
                        }
                    }
                    $as->save();

                    //更新群组信息
                    $g = array();
                    $ug_all = UserGroup::model()->findAllByAttributes(array('uid'=>$model->id));
                    foreach($ug_all as $ug_one){
                        if(empty($g[$ug_one->gid])){
                            $g[$ug_one->gid] = Group::model()->findByPk($ug_one->gid);
                        }
                        $ug_one->delete();
                        $g[$ug_one->gid]->count--;
                    }
                    if(isset($_POST['Group'])){
                        foreach($_POST['Group'] as $gid){
                            $ug = UserGroup::model()->findByAttributes(array('gid'=>$gid,'uid'=>$model->id));
                            if(empty($ug)){
                                $ug = new UserGroup;
                                $ug->uid = $model->id;
                                $ug->gid = $gid;
                                $ug->time = time();
                                $ug->status = 1;
                                $ug->save();
                                if(empty($g[$gid])){
                                    $g[$gid] = Group::model()->findByPk($gid);
                                }
                                $g[$gid]->count++;
                            }
                        }
                    }
                    foreach($g as $g_one){
                        if($g_one->count < 0){
                            $g_one->count = 0;
                        }
                        $g_one->save();
                    }
				}

				$this->redirect(array('view','id'=>$model->id));
			} else $profile->validate();
		}



		$this->render('create',array(
			'model'=>$model,
			'profile'=>$profile,
		));
	}


	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionUpdate()
	{
		$model=$this->loadModel();
		$profile=$model->profile;
		$this->performAjaxValidation(array($model,$profile));
        if(isset($_REQUEST['Profile'])){
            $profile_imei = Profile::model()->findByAttributes(array('imei'=>$_REQUEST['Profile']['imei']));
        }
        if(isset($profile_imei->user_id) && $profile_imei->user_id!=$profile->user_id){
            $profile->imei = $_REQUEST['Profile']['imei'];
            $profile->addError('imei','imei号重复');
        }else if(isset($_POST['User'])){
			$model->attributes=$_POST['User'];
			$profile->attributes=$_POST['Profile'];

            //超级用户权限
            $as = Authassignment::model()->findByAttributes(array('userid'=>$model->id));
            if($model->superuser==1){
                if(empty($as)){
                    $as = new Authassignment;
                    $as->itemname = 'Admin';
                    $as->userid = $model->id;
                    $as->bizrule = null;
                    $as->data = 'N;';
                }else{
                    $as->itemname = 'Admin';
                }
            }elseif($model->superuser==0){
                if(empty($as)){
                    $as = new Authassignment;
                    $as->itemname = 'Poster';
                    $as->userid = $model->id;
                    $as->bizrule = null;
                    $as->data = 'N;';
                }else{
                    $as->itemname = 'Poster';
                }
            }
            $as->save();

            //更新群组信息
            $g = array();
            $ug_all = UserGroup::model()->findAllByAttributes(array('uid'=>$model->id));
            foreach($ug_all as $ug_one){
                if(empty($g[$ug_one->gid])){
                    $g[$ug_one->gid] = Group::model()->findByPk($ug_one->gid);
                }
                $ug_one->delete();
                $g[$ug_one->gid]->count--;
            }
            if(isset($_POST['Group'])){
                foreach($_POST['Group'] as $gid){
                    $ug = UserGroup::model()->findByAttributes(array('gid'=>$gid,'uid'=>$model->id));
                    if(empty($ug)){
                        $ug = new UserGroup;
                        $ug->uid = $model->id;
                        $ug->gid = $gid;
                        $ug->time = time();
                        $ug->status = 1;
                        $ug->save();
                        if(empty($g[$gid])){
                            $g[$gid] = Group::model()->findByPk($gid);
                        }
                        $g[$gid]->count++;
                    }
                }
            }
            foreach($g as $g_one){
                if($g_one->count < 0){
                    $g_one->count = 0;
                }
                $g_one->save();
            }
			
			if($model->validate()&&$profile->validate()) {
				$old_password = User::model()->notsafe()->findByPk($model->id);
				if ($old_password->password!=$model->password) {
					$model->password=Yii::app()->controller->module->encrypting($model->password);
					$model->activkey=Yii::app()->controller->module->encrypting(microtime().$model->password);
				}
				$model->save();
				$profile->save();
				$this->redirect(array('view','id'=>$model->id));
			} else $profile->validate();
		}



		$this->render('update',array(
			'model'=>$model,
			'profile'=>$profile,
		));
	}


	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'index' page.
	 */
	public function actionDelete()
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$model = $this->loadModel();
			$profile = Profile::model()->findByPk($model->id);
			$profile->delete();
			$model->delete();
			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_POST['ajax']))
				$this->redirect(array('/user/admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}
	
	/**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($validate)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
        {
            echo CActiveForm::validate($validate);
            Yii::app()->end();
        }
    }
	
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
				$this->_model=User::model()->notsafe()->findbyPk($_GET['id']);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}
	
}